package com.wch.SpringBootTestDemo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wch.SpringBootTestDemo.webmagic.GgzyPageProcessor;
import com.wch.SpringBootTestDemo.webmagic.GithubRepoPageProcessor;
import com.wch.SpringBootTestDemo.webmagic.extension.DBTablePipeline;

import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.pipeline.FilePipeline;

@SuppressWarnings("all")
@Controller
@RequestMapping("/webmaigcController")
public class TestWebMagicController {

	@Autowired
	private DBTablePipeline dbtp;
	
	@RequestMapping("/testWebmagic")
	@ResponseBody
	public void StartWebMagicTest(){
		//Spider.create(new GithubRepoPageProcessor()).addUrl("https://github.com/code4craft").thread(5).run();
		
		//写入文件
	//	Spider.create(new GithubRepoPageProcessor()).addUrl("http://my.oschina.net/flashsword/blog").addPipeline(new FilePipeline("d:/webmagic/file")).run();
		
		//写入数据库
		Spider.create(new GithubRepoPageProcessor()).addUrl("http://my.oschina.net/flashsword/blog").addPipeline(dbtp).run();

	}
	
	@RequestMapping("/testWebmagic2")
	@ResponseBody
	public void StartWebMagicTest2(){
		//Spider.create(new GithubRepoPageProcessor()).addUrl("https://github.com/code4craft").thread(5).run();
		
		//写入文件
	//	Spider.create(new GithubRepoPageProcessor()).addUrl("http://my.oschina.net/flashsword/blog").addPipeline(new FilePipeline("d:/webmagic/file")).run();
		
		//写入数据库自定义表和序列
		dbtp.newInstance("X_WCH_TABLE", "X_WCH_SEQ");
		Spider.create(new GithubRepoPageProcessor()).addUrl("http://my.oschina.net/flashsword/blog").addPipeline(dbtp).run();

	}
	
	@RequestMapping("/ggzyWebmagic")
	@ResponseBody
	public void GgzyWebMagicTest2(){
		//Spider.create(new GithubRepoPageProcessor()).addUrl("https://github.com/code4craft").thread(5).run();
		
		//写入文件
	//	Spider.create(new GithubRepoPageProcessor()).addUrl("http://my.oschina.net/flashsword/blog").addPipeline(new FilePipeline("d:/webmagic/file")).run();
		
		//写入数据库自定义表和序列
	//	Spider.create(new GgzyPageProcessor()).addUrl("http://deal.ggzy.gov.cn/ds/deal/dealList.jsp").addPipeline(dbtp).run();
		
		//	Spider.create(new GgzyPageProcessor()).addUrl("http://deal.ggzy.gov.cn/ds/deal/dealList.jsp").addPipeline(new FilePipeline("d:/webmagic/file2")).run();
		dbtp.initTableField();
		for(int i =1;i<2246;i++){
			String tarUrl = "http://deal.ggzy.gov.cn/ds/deal/dealList_find.jsp?TIMEBEGIN_SHOW=2019-02-13&TIMEEND_SHOW=2019-02-22&TIMEBEGIN=2019-02-13&TIMEEND=2019-02-22&DEAL_TIME=02&DEAL_CLASSIFY=00&DEAL_STAGE=0000&DEAL_PROVINCE=0&DEAL_CITY=0&DEAL_PLATFORM=0&DEAL_TRADE=0&isShowAll=1&PAGENUMBER="+i+"&FINDTXT=&validationCode=";
			//	Spider.create(new GgzyPageProcessor()).addUrl(tarUrl).addPipeline(new FilePipeline("d:/webmagic/file2")).run();
				Spider.create(new GgzyPageProcessor()).addUrl(tarUrl).addPipeline(dbtp).run();
		}
		
	}
	
	
	
	//为将一些方法设为私有，故下面测试就不用了
	/*@RequestMapping("/testDBTablePipeline")
	@ResponseBody
	public String testDBTablePipeline(){
		dbtp.createTableInfo();
		for(int i=0;i<10;i++){
			dbtp.insertData("这是第"+i+"条测试记录");
		}
		return "结束";		
	}
	
	@RequestMapping("/testDBTablePipeline2")
	@ResponseBody
	public String testDBTablePipeline2(){
		dbtp.newInstance("x_wch_test", "x_wch_test");
		dbtp.createTableInfo();
		for(int i=0;i<10;i++){
			dbtp.insertData("这是第"+i+"条测试记录");
		}
		return "结束";		
	}*/
	
	
}
